package com.zb.network.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zb.entity.*;
import com.zb.network.service.ConNetworkEmpService;
import com.zb.result.PageUtil;
import com.zb.service.ViewNetworkEmpAccountService;
import com.zb.service.ViewNetworkEmpDetailService;
import com.zb.service.ZjAccountService;
import com.zb.service.ZjWorkOrderService;
import com.zb.view.ViewNetworkEmpAccount;
import com.zb.view.ViewNetworkEmpDetail;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author WEN
 * @Date 2023/11/29 0029
 **/
@Service
public class ConNetworkEmpServiceImpl implements ConNetworkEmpService {
    @Resource
    private ViewNetworkEmpDetailService viewNetworkEmpDetailService;
    @Resource
    private ZjWorkOrderService workOrderService;
    @Resource
    private ViewNetworkEmpAccountService viewNetworkEmpAccountService;

    // 获取服务商派单工程师
    @Override
    public Map<String, Object> getNetworkDispatchEngineer(AccounLoginInfo loginInfo, SelectVo selectVo) {
        PageUtil.setPageLimitMobile(selectVo);
        List<ViewNetworkEmpDetail> networkEmpDetails = viewNetworkEmpDetailService.getNetworkEmpPageSelect(loginInfo.getNetworkId(), selectVo);
        if (CollectionUtil.isEmpty(networkEmpDetails)) {
            return PageUtil.pageList(selectVo.getPage(), selectVo.getLimit(), 0L, new ArrayList<>());
        }
        List<Long> networkEmpIds = networkEmpDetails.stream().map(ViewNetworkEmpDetail::getId).toList();
        List<QtyVo> qtyVos = new ArrayList<>();
        List<ViewNetworkEmpAccount> accountList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(networkEmpIds)) {
            qtyVos.addAll(workOrderService.getNetworkEmpOrderQtyByNetworkAndNetworkEmpIds(loginInfo.getNetworkId(), networkEmpIds));
            accountList.addAll(viewNetworkEmpAccountService.getByIds(networkEmpIds));
        }
        List<SelectEmpVo> voList = networkEmpDetails.stream().map(emp -> {
            return new SelectEmpVo(emp.getId(), "", emp.getAccountPhone(), emp.getAccountName(),
                    qtyVos.stream().filter(f -> Objects.equals(f.getId(), emp.getId())).map(QtyVo::getQty).findFirst().orElse(0),
                    accountList.stream().filter(f -> Objects.equals(f.getId(), emp.getId())).map(ViewNetworkEmpAccount::getAvatar).findFirst().orElse(""));
        }).toList();
        return PageUtil.pageList(selectVo.getPage(), selectVo.getLimit(), viewNetworkEmpDetailService.getNetworkEmpPageSelectCount(loginInfo.getNetworkId(), selectVo), voList);
    }
}
